package com.fengba.nanzhi.tabbar04.wz;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;

import org.json.JSONException;
import org.json.JSONObject;

public class ProfitActivity extends AppCompatActivity {
    private String user_id,cash_money,headImg;
    private TextView ProfitCash_money,ProfitMoney,minMoney,TXmoneySXF2,TXmoney,maxMoney,TXCashComment,ProUser_name,Pro_Account,Pro_Money,Pro_SXF,Pro_Final_Money,Pro_Card,Pro_Time,Pro_Time_2;
    private Button ProQBBtn,ProfitBtn,proFou,proShi,Pro_OK,MXBtn;
    private EditText ProfitNumber;
    private JSONObject jsob;
    private double TXmoneySXF,number1,minMoneyd,maxMoneyd;
    private RelativeLayout proQDK,Pro_gen,Pro_OKJM;
    private ImageView Pro_HeadImg;
    private ImageButton Profitfh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wz_activity_profit);
        ProfitCash_money = findViewById(R.id.ProfitNumber1);
        //最小提现金额
        minMoney = findViewById(R.id.minMoney);
        //提现手续费
        TXmoneySXF2 = findViewById(R.id.TXmoneySXF);
        //最大提现金额
        maxMoney = findViewById(R.id.maxMoney);
        //全部提现
        ProQBBtn = findViewById(R.id.ProQBBtn);
        //用户输入金额
        ProfitNumber = findViewById(R.id.ProfitNumber);
        //提现5w
        TXmoney = findViewById(R.id.TXmoney);
        //实际到账金额
        ProfitMoney = findViewById(R.id.ProfitMoney);
        //说明
        TXCashComment = findViewById(R.id.TXCashComment);
        //提交按钮
        ProfitBtn = findViewById(R.id.ProfitBtn);
        //确定框
        proQDK = findViewById(R.id.proQDK);
        //否
        proFou = findViewById(R.id.proFou);
        //是
        proShi = findViewById(R.id.proShi);
        //背景试图
        Pro_gen = findViewById(R.id.Pro_gen);
        //成功界面
        Pro_OKJM = findViewById(R.id.Pro_OKJM);
        //完成那妞
        Pro_OK = findViewById(R.id.Pro_OK);
        //用户名
        ProUser_name = findViewById(R.id.ProUser_name);
        //手机号
        Pro_Account = findViewById(R.id.Pro_Account);
        //提现金额
        Pro_Money = findViewById(R.id.Pro_Money);
        //提现手续费
        Pro_SXF = findViewById(R.id.Pro_SXF);
        //实际到账
        Pro_Final_Money = findViewById(R.id.Pro_Final_Money);
        //到账卡号
        Pro_Card = findViewById(R.id.Pro_Card);
        //到账时间
        Pro_Time = findViewById(R.id.Pro_Time);
        //处理时间
        Pro_Time_2 = findViewById(R.id.Pro_Time_2);
        //用户头像
        Pro_HeadImg = findViewById(R.id.Pro_HeadImg);
        //OK
        Pro_OK = findViewById(R.id.Pro_OK);
        //明细
        MXBtn = findViewById(R.id.MXBtn);
        init();
        ProQBBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProfitNumber.setText(ProfitCash_money.getText().toString());
            }
        });

        //返回
        Profitfh.findViewById(R.id.Profitfh);
        Profitfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        TextWatcher textWatcher = new TextWatcher() {
            // 输入文本之前的状态
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            // 输入文本中的状态
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String number = charSequence.toString();

                if(!number.equals("")) {
                    //number1 = Integer.parseInt(number);
                    number1 = Double.parseDouble(number);
                }else{
                    ProfitMoney.setText("0");
                }
                if(!number.equals("")&&number1-TXmoneySXF>0) {
                    ProfitMoney.setText((number1-TXmoneySXF)+"");
                }
            }
            // 输入文本之后的状态
            @Override
            public void afterTextChanged(Editable editable) {
            }
        };
        ProfitNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
        //添加键盘输入事件
        ProfitNumber.addTextChangedListener(textWatcher);

        //点击提交
        ProfitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ProfitCash_money1 = ProfitCash_money.getText().toString();
                double ProfitCash_moneyd = Double.parseDouble(ProfitCash_money1);
                String SRnumber = ProfitNumber.getText().toString();

                if(!SRnumber.trim().equals("")){
                    double SRnumberd = Double.parseDouble(SRnumber);
                if(SRnumberd<minMoneyd){
                    showToast(ProfitActivity.this,"最低提现金额不得小于"+minMoneyd+"元");
                }else if(SRnumberd>maxMoneyd*10000){
                    showToast(ProfitActivity.this,"单卡单日最高提现"+maxMoneyd+"w元");
                }else if(SRnumberd>ProfitCash_moneyd){
                    showToast(ProfitActivity.this,"用户余额不足!");
                }else{
                    Pro_gen.setBackground(getResources().getDrawable(R.drawable.wz_70_00));
                    proQDK.setVisibility(View.VISIBLE);
                }
                }else{
                    showToast(ProfitActivity.this,"请输入正确的金额!");
                }
            }
        });
        proFou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Pro_gen.setBackground(null);
                proQDK.setVisibility(View.GONE);
            }
        });
        proShi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                OK();
            }
        });

        Pro_OK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        MXBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ProfitActivity.this,ProfitMXActivity.class);
                intent.putExtra("user_id",user_id);
                startActivity(intent);
            }
        });

    }


    public void  init(){
        Object user_info = Preferences.getParam(ProfitActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());

            //账号
            user_id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type1006(user_id, new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                JSONObject UserObj = null;
                try {
                    UserObj = (JSONObject) jsonObject.get("data");
                    cash_money = UserObj.get("cash_money").toString();

                    ProfitCash_money.setText(cash_money);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {

            }
        });

        OKManager.getInstance().type1014(new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                try {
                    String code = jsonObject.get("code").toString();
                    if(code.trim().equals("0")) {
                        jsob = (JSONObject) jsonObject.get("data");
                        String TXmoneySXF1 = jsob.get("TXmoneySXF").toString();
                        TXmoneySXF = Double.parseDouble(TXmoneySXF1);

                        minMoney.setText(jsob.get("minMoney").toString() + "元");
                        minMoneyd = Double.parseDouble(jsob.get("minMoney").toString());
                        TXmoneySXF2.setText(TXmoneySXF1 + "元");
                        TXmoney.setText(jsob.get("TXmoney").toString() + "w");
                        maxMoney.setText(jsob.get("maxMoney").toString() + "w");
                        maxMoneyd = Double.parseDouble(jsob.get("maxMoney").toString());
                        TXCashComment.setText(jsob.get("TXCashComment").toString());
                    }else{
                        showToast(ProfitActivity.this,jsonObject.get("meaasge").toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {
                showToast(ProfitActivity.this,"网络错误");
            }
        });
    }
    public void OK (){
        Object user_info = Preferences.getParam(ProfitActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());

            //账号
            user_id = myJson.get("id").toString();
            headImg = myJson.get("headimg").toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type1015(user_id, ProfitNumber.getText().toString(), new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    JSONObject jsonObject1 = (JSONObject) jsonObject.get("data");
                    String user_name = jsonObject1.get("name").toString();
                    String account = jsonObject1.get("account").toString();
                    String money = jsonObject1.get("money").toString();
                    String service_charge = jsonObject1.get("service_charge").toString();
                    String card_number = jsonObject1.get("card_number").toString();
                    String createtime = jsonObject1.get("createtime").toString();
                    ProUser_name.setText(user_name);
                    //手机号
                    Pro_Account.setText(account);
                    //提现金额
                    Pro_Money.setText(money);
                    //提现手续费
                    Pro_SXF.setText(service_charge);
                    //实际到账
                    Pro_Final_Money.setText((Double.parseDouble(money)-Double.parseDouble(service_charge))+"");
                    //到账卡号
                    Pro_Card.setText(card_number);
                    //提现时间
                    Pro_Time.setText(createtime);
                    //处理时间
                    Pro_Time_2.setText(TXCashComment.getText());

                    Glide.with(ProfitActivity.this).load(headImg).into(Pro_HeadImg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Pro_gen.setBackground(getResources().getDrawable(R.drawable.wz_70_00));
                Pro_OKJM.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFailure(String string) {
                showToast(ProfitActivity.this,"网络错误");

            }
        });
    }
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
